<?php

namespace app\admin\controller;

use think\Db;
use think\Request;


class Category extends Admin
{

    // 查询数据
    public function index(Request $request)
    {
        $a = '0,';

        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('category')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('category')->field(true)->where('display',1)->where($where)->where('path','like',$a)->paginate(4);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('category/categoryList');
        

    }
    // 查询数据
    public function disable(Request $request)
    {
        $a = '0,';

        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('category')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('category')->field(true)->where('display',2)->where($where)->where('path','like',$a)->paginate(4);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('category/disable');
        

    }


    // 保存一级分类数据
    public function save(Request $req)
    {

        // 接收数据
        $data = $req->get();


        // 填充数据
        $data['pid'] = 0;
        $data['path'] = '0,';


        $cate = DB::table('category')->select();

        foreach ($cate as $k => $v) {

            if ($v['name'] == $data['name']) {
                                    
                $info['status'] = false;
                $info['msg'] = '分类名重复';

                return json($info);
            }
            
        }


        // 执行sql
        $result = Db::table('category')->data($data)->insert();


        // 返回结果
        if ($result > 0) {

            
            $info['status'] = true;
            $info['msg'] = '添加成功';

            return json($info);

        } else {

                    
            $info['status'] = false;
            $info['msg'] = '添加失败';

            return json($info);


        }
    }

    // 添加子分类
    public function addcation($id)
    {

        $data = Db::table('category')->find($id);
     
        $info['data'] = $data;

        // 跳转
        return json($info);

    }

    // 保存数据
    public function doaddcation(Request $req)
    {

        $data = $req->get();


        $id = $data['id'];

        $res = DB::table('category')->where('id',$id)->find();


        $cate = DB::table('category')->select();

        

        foreach ($cate as $k => $v) {

            if ($v['name'] == $data['name']) {
                                   
                $info['status'] = false;
                $info['msg'] = '分类名重复';

                return json($info);

            }
        }  


        $a = $res['path'].$id.',';

        // 凑完整数据
        $b = [
        'name' => $data['name'],
        'pid' => $id,
        'path' => $a,
        'display' => $data['display']
        ];

        // 执行sql
        $result = Db::table('category')->data($b)->insert();

        // 返回结果
        if ($result > 0) {

            
            $info['status'] = true;
            $info['msg'] = '添加成功';

            return json($info);

        } else {

                    
            $info['status'] = false;
            $info['msg'] = '添加失败';

            return json($info);


        }
    }



    // 查看子分类
    public function checkcation(Request $request,$id=null)
    {
        $a = '%,'.$id.',';

        $res = $request->get();
        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('category')->where($where)->paginate(5)->count();
            if ($count == 0) {
                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('category')->field(true)->where('display',1)->where($where)->where('path','like', $a)->where('path','like',$a)->paginate(4);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        $result = Db::table('category')->where('id',$id)->find();

        // var_dump($result);die;

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);
        $this->assign('name', $result);
       

        // 返回
        return $this->fetch('category/checkcation');
    }


    // 查询语句
    public function find($id)
    {
        // 查询
        $data = Db::table('category')->find($id);

        $info['data'] = $data;

        // 跳转
        return json($info);

    }


    // 修改分类
    public function updata(Request $request)
    {

        // 接收数据
        $data = $request->get();

        // 获取id
        $id = $request->get('id');

        $data['pid'] = 0;
        $data['path'] = '0,';


        // 执行sql语句
        $result = Db::table('category')->where('id', $id)->update($data);

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }
    }


    // 修改子分类
    public function editcation(Request $request)
    {

        // 接收数据
        $data = $request->get();


        // 获取id
        $id = $request->get('id');


        // 执行sql语句
        $result = Db::table('category')->where('id', $id)->update($data);

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }
    }



    // 禁用分类
    public function del_category(Request $request)
    {
        $id = $request->get('id');

        $data = DB::table('category')->find($id);

        // 改变值
        $data['display'] = $data['display'] == 2?'1':'2';

        $res = $data['display'] == 1?'已启用':'已禁用';


        // 更新数据
        $sql = DB::table('category')->where('id',$id)->update($data);


        // 返回结果
        if ($sql > 0) {
           
            $info['status'] = true;
            $info['msg'] = $res;

            return json($info);

        }

    }



}


